// 导出页面为PDF格式
import html2Canvas from "html2canvas";
import JsPDF from "jspdf";
export default {
  getPdf(title) {
    html2Canvas(document.querySelector("#pdfDom"), {
      allowTaint: true,
      useCORS: true,
      scale: 2, // 提升画面质量，但是会增加文件大小
      dpi: window.devicePixelRatio * 1,
    }).then(function (canvas) {
      /* 导出分页处理 */
      // let contentWidth = canvas.width;
      // let contentHeight = canvas.height;
      // alert(canvas.height);
      // alert(contentWidth);
      // let pageHeight = (contentWidth / 592.28) * 841.89;
      // let leftHeight = contentHeight;
      // let position = 0;
      // let imgWidth = 595.28;
      // let imgHeight = (592.28 / contentWidth) * contentHeight;
      // let pageData = canvas.toDataURL("image/jpeg", 1.0);
      // let PDF = new JsPDF("", "pt", "a4");
      // if (leftHeight < pageHeight) {
      //   PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
      // } else {
      //   while (leftHeight > 0) {
      //     PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
      //     leftHeight -= pageHeight;
      //     position -= 841.89;
      //     if (leftHeight > 0) {
      //       PDF.addPage();
      //     }
      //   }
      // }
      // PDF.save(title + ".pdf");

      /* 导出不分页处理 */
      let contentWidth = canvas.width;
      let contentHeight = canvas.height;
      const pageData = canvas.toDataURL("image/jpeg", 1.0);
      const pdfWidth = ((contentWidth + 10) / 2) * 0.95;
      const pdfHeight = ((contentHeight + 200) / 2) * 0.75; // 500为底部留白
      const imgWidth = pdfWidth;
      const imgHeight = (contentHeight / 2) * 0.75; // 内容图片这里不需要留白的距离
      const PDF = new JsPDF("", "pt", [pdfWidth + 50, pdfHeight + 100]);
      PDF.addImage(pageData, "jpeg", 20, 20, imgWidth, imgHeight);
      PDF.save(title + ".pdf");
    });
    // };
  },
};
